<template>
    <div class="centerNewIndex">
      <div class="centerNewIndex--header">
        钥匙柜
      </div>
      <div class="centerNewIndex--tabs">
        <BtnGroupDevideTab v-if="tabList.length" style="margin: 16px 0 16px 0px;" :tabList="tabList" :tabActive="tabActive" @change="typeChangeAction"/>
      </div>
      <div class="centerNewIndex--content">
        <div class="centerNewIndex--content__item" :class="{borrow: item.borrow}" v-for="(item, index) in tableData" :key="index">
          <div class="name">{{item.lockerName}}</div>
        </div>
      </div>
    </div>
</template>

<script>
import BtnGroupDevideTab from '@/views/armyScreen/comp/BtnGroupDevideTab.vue'
import {random14} from '@/util/utils.js'
import {keyStateService} from '@/api/armyScreenApiV2.js'
import useCommonMethods from '@/mixin/useCommonMethods.js'
import $bus from "@/util/bus";

export default {
  inject: ['screenContext'],
  components: {
    BtnGroupDevideTab
  },
  data() {
    return {
      timer: null,
      sid: 'chart_' + random14(),
      tabList: [
        {
          label: '全部',
          value: ''
        },
        {
          label: '1号柜',
          value: 'E'
        },
        {
          label: '2号柜',
          value: 'GB'
        },
      ],
      tabActive: '',
      tableData: []
    }
  },
  setup() {
    const {dictByTypeService} = useCommonMethods()
    return {
      dictByTypeService
    }
  },
  async mounted() {
    // 钥匙柜列表-lockerList
    // 钥匙借取记录时间范围-keyUseTimeScope
    const {data} = await this.dictByTypeService('lockerList')
    const dictArr = data?.list || []
    dictArr.forEach(item => {
      item.value = item.dictCode
      item.label = item.dictValue
    })
    const newDictArr = dictArr.filter(item => {
      return item.dictCode !== 'all'
    })
    this.tabList = newDictArr
    if (dictArr[0]) { // 需要从数据字典获取数据，如果没有类型，将无法展示
      this.tabActive = newDictArr[0].dictCode
      this.getKeyState()
      this.autoChangeTab()
    }
    $bus.on('reRequest', this.getKeyState)
  },
  beforeUnmount() {
    $bus.off('reRequest', this.getKeyState)
    if (this.timer) {
      clearTimeout(this.timer)
    }
  },
  methods: {
    // 自动切换状态
    autoChangeTab() {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      this.timer = setTimeout(() => {
        const tempArr = this.tabList.filter(item => {
          return item.dictCode !== this.tabActive
        })
        this.tabActive = tempArr[0].dictCode
        this.autoChangeTab()
        this.getKeyState()
      }, 10 * 1000)
    },
    // 获取颜色的样式
    getColorStr (str) {
      if (str === '已归还') {
        return 'color: #00E279;'
      } else if (str === '已借出') {
        return 'color: #EE4A45;'
      }
      return ''
    },
    formatDateStr (dateStr) {
      if (dateStr) {
        return dateStr.substring(0,10)
      } else {
        return '--'
      }
    },
    tabChangeAction(value) {
      this.btnActive = value
      this.getKeyState()
    },
    typeChangeAction(value) {
      this.tabActive = value
      if (this.timer){
        clearTimeout(this.timer)
        this.autoChangeTab()
      }
      this.getKeyState()
    },
    getKeyState() {
      const paramObj = {
        "lockerIndexCode": this.tabActive,
        // "lockerIndexCode": "all,locker_1,locker_2"
      }
      keyStateService(paramObj).then(res => {
        console.log(res)
        this.tableData = res?.data || []
      })
    },
    changeTab(tab) {
      this.tabActive = tab
      this.cameraOnOfflineBySchool()
    },
  }
}
</script>

<style lang="scss" scoped>
.centerNewIndex{
  &--header{
    width: 930px;
    height: 40px;
    line-height: 40px;
    background-image: url('@/assets/images/armyScreen/warehouse/long标题.png');
    background-size: cover;
    padding-left: 48px;

    font-family: PingFangSC-Medium;
    font-size: 18px;
    color: #DBECFB;
    letter-spacing: 2px;
    font-weight: 500;
  }
  &--tabs{}
  &--content{
    display: grid;
    grid-template-columns: repeat(5, 176px);
    grid-template-rows: repeat(6, 128px);
    grid-gap: 12px;
    &__item{
      width: 176px;
      height: 128px;
      background-image: url('@/assets/images/armyScreen/warehouse/钥匙箱-有.png');
      background-size: cover;
      position: relative;
      &.borrow{
        background-image: url('@/assets/images/armyScreen/warehouse/钥匙箱-无.png');
      }
      .name{
        text-align: center;
        font-family: PingFangSC-Medium;
        font-size: 18px;
        color: #DBECFB;
        letter-spacing: 2px;
        font-weight: 500;
        margin-top: 20px;
      }
    }
  }
}
</style>
